package com.tools.genxsd.entity;

import cn.hutool.core.text.CharSequenceUtil;
import lombok.Data;
import lombok.ToString;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;

import java.util.Objects;

import static cn.hutool.core.text.StrPool.DASHED;
import static cn.hutool.core.text.StrPool.SLASH;
import static com.tools.genxsd.constant.RegexConstant.DOUBLE_DOT_REGEX;
import static com.tools.genxsd.constant.RegexConstant.PARENTHESIS_REGEX;
import static com.tools.genxsd.constant.SysConstant.*;

@Data
@ToString
public class XElement {

    private XSSFRow row;

    /**
     * 节点等级
     */
    private int level = -2;
    /**
     * 中文名称
     */
    private String notes;
    /**
     * 节点名，即报文要素
     */
    private String name;
    /**
     * 节点符号
     */
    private String tag;

    /**
     * 最大个数 有n
     */
    private String maxOccurs;

    /**
     * 最小个数
     */
    private String minOccurs;

    /**
     * 类型
     */
    private String type;

    /**
     * 或
     */
    private boolean or;

    /**
     * 记录当前节点是Excel表中第几行
     */
    private int index = 0;

    /**
     * 类型注释
     */
    private String typeDescribe;

    public XElement() {
    }

    public XElement(XSSFRow row) {
        this.row = row;
        XSSFCell orCell = row.getCell(1);
        or = Objects.nonNull(orCell) && Objects.equals(orCell.getStringCellValue(), OR_FILED);
        String name = row.getCell(2).toString().trim();
        int index = setLevelAndGetIndex(name);
        this.name = name.substring(index).replaceAll(SPACE, NULL_STR);

        XSSFCell notesCell = row.getCell(3);
        this.notes = Objects.isNull(notesCell) ? null : notesCell.toString().trim();

        String tags = row.getCell(4).toString().trim();
        this.tag = tags.substring(1, tags.length() - 1);

        XSSFCell cell = row.getCell(5);
        if (Objects.nonNull(cell) && CharSequenceUtil.isNotBlank(cell.toString())) {
            String attr = cell.toString().trim();
            String[] split = attr.split(DOUBLE_DOT_REGEX);
            this.minOccurs = split[0].substring(1);
            this.maxOccurs = split[1].substring(0, split[1].length() - 1);
        }

        XSSFCell type = row.getCell(6);
        this.type = Objects.isNull(type) ? null : type.toString().trim();

        XSSFCell typeDescCell = row.getCell(7);
        this.typeDescribe = Objects.isNull(typeDescCell) ? null : typeDescCell.toString();
    }

    /**
     * 设置节点等级并返回当前节点真正报文要素的指针位置
     * @param info
     * @return
     */
    private int setLevelAndGetIndex(String info) {
        int i = info.lastIndexOf(DASHED);
        this.level = Objects.equals(info, MESSAGE_ROOT) ? -1 : i + 1;

        return i + 1;
    }

    public String getType() {
        if (type.contains(PARENTHESIS)) {
            return type.split(PARENTHESIS_REGEX)[0].trim();
        }

        if (type.contains(CHINA_PARENTHESIS)) {
            return type.split(CHINA_PARENTHESIS)[0].trim();
        }
        return type;
    }

    public String getName() {
        if (name.contains(SLASH)) {
            return name.replaceAll(SLASH, NULL_STR);
        }
        return name;
    }
}
